package idiet.dao.hibernate;

import idiet.bean.FoodIngredient;
import idiet.dao.SqlStatementHelper;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("foodIngredientDao")
public class FoodIngredientDao {
	@Autowired
	SessionFactory sessionFactory;
	@Autowired
	SqlStatementHelper sqlStatementHelper;
	/*
	 * query ingredient of food's material by food material id
	 */
	@SuppressWarnings("unchecked")
	public List<FoodIngredient> getFoodIngredientByMaterialId(int materialId){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredient.class)
				.add(Restrictions.eq("food_id", materialId))
				.list();
	}
	public FoodIngredient getFoodIngredientByFoodIngredientId(int foodIngredientId){
		return (FoodIngredient) this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredient.class)
				.add(Restrictions.eq("id",foodIngredientId))
				.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<FoodIngredient> getFoodIngredientsByMaterialName(String materialName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredient.class)
				.add(Restrictions.like("name", materialName))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodIngredient> getFoodIngredients(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodIngredient.class)
				.list();
	}
	public void updateFoodIngredient(FoodIngredient foodIngredient){
		this.sessionFactory
		.getCurrentSession()
		.update(foodIngredient);
	}
	public void deleteFoodIngredient(FoodIngredient foodIngredient){
		this.sessionFactory
		.getCurrentSession()
		.delete(foodIngredient);
	}
}
